<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/11
 * Time: 14:18
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\shop\validate\UserLevel;
use app\common\builder\ZBuilder;
use app\shop\model\UserLevelModel;

class User extends Admin
{
    public function index()
    {
        // 会员列表
        return ZBuilder::make('form')
            ->fetch();
    }

    // 会员详情
    public function userDetail()
    {

    }

    // 会员等级
    public function level()
    {
        $list = UserLevelModel::getList();
        return ZBuilder::make('table')
            ->setPageTitle('会员等级')
            ->addColumn('id','ID')
            ->addColumn('level_name','等级名称')
            ->addColumn('rate','享受折扣')
            ->addColumn('right_button','操作')
            ->addRightButton('edit',['href' => url('leveledit',['id' => '__id__'])])
            ->addRightButton('delete',['href' => url('leveldel',['id' => '__id__'])])
            ->addTopButton('add_level',['title'=>'新增','icon'=>'fa fa-plus-circle','href'=>url('levelAdd'),'class'=>'btn btn-primary'])
            ->setRowList($list)
            ->fetch();
    }

    // 添加会员等级
    public function levelAdd()
    {
        if(request()->isPost()){
            $this->storeLevel();
        }
        return ZBuilder::make('form')
            ->setPageTitle('添加会员等级')
            ->setUrl(url('levelAdd'))
            ->isAjax(false)
            ->addText('level_name','会员名称')
            ->addText('rate','折扣','单位为%，如不打折则填写100')
            ->fetch();
    }

    // 编辑会员等级
    public function levelEdit($id = 0)
    {
        if(request()->isPost()){
            $this->storeLevel();
        }
        $levelInfo = UserLevelModel::getOne($id);
        return ZBuilder::make('form')
            ->setPageTitle('添加会员等级')
            ->addHidden('id')
            ->setUrl(url('levelEdit'))
            ->isAjax(false)
            ->addText('level_name','会员名称')
            ->addText('rate','折扣','单位为%，如不打折则填写100')
            ->setFormData($levelInfo)
            ->fetch();
    }

    // 删除会员等级
    public function levelDel($id)
    {
        $res = UserLevelModel::del($id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    private function storeLevel()
    {
        $data = input('post.');
        $id = input('param.id', 0);
        $validate = new UserLevel();
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $res = UserLevelModel::store($data, $id);
        if($res){
            $this->success('保存成功','level');
        }else{
            $this->error('保存失败');
        }
    }


    // 充值记录
    public function recharge()
    {

    }

    // 提现申请
    public function withdraw()
    {

    }

}